﻿using System;
using System.Collections.Generic;
using System.Dynamic;
using System.Globalization;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Html;
using System.Web.Routing;

namespace LKVMLMADMIN.Extension
{
    public static class HtmlHelperExtensions
    {
        public static MvcHtmlString EnumDropDownListFor<TModel, TProperty, TEnum>(
                 this HtmlHelper<TModel> htmlHelper,
                 Expression<Func<TModel, TProperty>> expression,
                 TEnum selectedValue)
             where TEnum : struct, IConvertible
        {
            var values = Enum.GetValues(typeof(TEnum))
                .Cast<TEnum>().ToArray();

            var items = values
                .Select(x => new
                {
                    Text = x.ToString(CultureInfo.InvariantCulture),
                    Value = Convert.ChangeType(x, x.GetTypeCode()),
                    Selected = (x.Equals(selectedValue))
                })
                .Select(x => new SelectListItem
                {
                    Text = x.Text,
                    Value = x.Value == null
                        ? ""
                        : x.Value.ToString(),
                    Selected = x.Selected
                });

            return htmlHelper.DropDownListFor(expression, items);
        }
        public static string generateRandomString(int length = 0, int isAlphanumeric = 0)
        {          
            string alphabets = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
            string small_alphabets = "abcdefghijklmnopqrstuvwxyz";
            string numbers = "1234567890";
 
            string characters = numbers;
            if (isAlphanumeric == 1)
            {
                characters += alphabets + small_alphabets + numbers;
            }
            string otp = string.Empty;
            for (int i = 0; i < length; i++)
            {
                string character = string.Empty;
                do
                {
                    int index = new Random().Next(0, characters.Length);
                    character = characters.ToCharArray()[index].ToString();
                } while (otp.IndexOf(character) != -1);
                otp += character;
            }
            return otp;
        }
    }

}